package com.java110.mall.cart.cmd.storeOrder;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.storeOrder.StoreOrderDiscountDto;
import com.java110.intf.cart.IStoreOrderDiscountInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

@Java110Cmd(serviceCode = "storeOrder.queryAdminOrderDiscount")
public class QueryAdminOrderDiscountCmd extends Cmd {

    @Autowired
    private IStoreOrderDiscountInnerServiceSMO storeOrderDiscountInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        super.validateAdmin(context);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        StoreOrderDiscountDto storeOrderDiscountDto = BeanConvertUtil.covertBean(reqJson, StoreOrderDiscountDto.class);
        storeOrderDiscountDto.setDiscountObjType(StoreOrderDiscountDto.DISCOUNT_OBJ_TYPE_ORDER);
        storeOrderDiscountDto.setDiscountObjId(reqJson.getString("orderId"));

        int count = storeOrderDiscountInnerServiceSMOImpl.queryStoreOrderDiscountsCount(storeOrderDiscountDto);

        List<StoreOrderDiscountDto> storeOrderDiscountDtos = null;
        if (count > 0) {
            storeOrderDiscountDtos = storeOrderDiscountInnerServiceSMOImpl.queryStoreOrderDiscounts(storeOrderDiscountDto);
        } else {
            storeOrderDiscountDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeOrderDiscountDto.getRow()), count, storeOrderDiscountDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);

    }
}
